package com.lagou.service;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcRequestEncoder extends MessageToByteEncoder {

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) {
        if (msg instanceof RpcRequest) {
            byte[] bytes = JSON.toJSONBytes(msg);
            out.writeInt(bytes.length);
            out.writeBytes(bytes);
        }
    }

}
